package com.getpebble.android.notifications;

import android.app.Notification;
import android.support.v4.app.NotificationCompatSideChannelService;
import com.getpebble.android.common.core.trace.Analytics.MobileAppBehavior;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.ProcessUtil;
import com.getpebble.android.framework.notification.PblNotificationProcessor;
import com.getpebble.android.notifications.model.PblNotification;
import com.getpebble.android.notifications.model.PblNotification.Source;

public class PblNotificationSideChannelService extends NotificationCompatSideChannelService {
    private static final String TAG = PblNotificationSideChannelService.class.getSimpleName();

    public void notify(final String packageName, int id, String tag, final Notification notification) {
        try {
            Trace.debug(TAG, "notify: packageName = " + packageName + " id = " + id + " tag = " + tag);
            ProcessUtil.runWithClearedIdentity(new Runnable() {
                public void run() {
                    if (notification == null) {
                        Trace.error(PblNotificationSideChannelService.TAG, "Failed to create PblNotification: notification was null");
                    } else if (packageName == null) {
                        Trace.error(PblNotificationSideChannelService.TAG, "Failed to create PblNotification: packageName was null.");
                    } else {
                        PblNotificationProcessor.processNotification(PblNotification.from(notification, packageName, Source.SIDECHANNEL));
                    }
                }
            });
        } catch (Exception e) {
            Trace.error(TAG, "Error handling sidechannel notification", e);
            MobileAppBehavior.logNotificationServiceError(TAG, false, e);
        } catch (Throwable t) {
            Trace.error(TAG, "Unrecoverable error occurred handling sidechannel notification", t);
            MobileAppBehavior.logNotificationServiceError(TAG, true, t);
        }
    }

    public void cancel(final String packageName, final int id, final String tag) {
        try {
            Trace.debug(TAG, "cancel: packageName = " + packageName + " id = " + id + " tag = " + tag);
            ProcessUtil.runWithClearedIdentity(new Runnable() {
                public void run() {
                    PblNotificationProcessor.processNotificationRemoval(id, tag, packageName, null);
                }
            });
        } catch (Exception e) {
            Trace.error(TAG, "Error handling sidechannel notification", e);
            MobileAppBehavior.logNotificationServiceError(TAG, false, e);
        } catch (Throwable t) {
            Trace.error(TAG, "Unrecoverable error occurred handling sidechannel notification", t);
            MobileAppBehavior.logNotificationServiceError(TAG, true, t);
        }
    }

    public void cancelAll(final String packageName) {
        try {
            Trace.debug(TAG, "cancelAll: packageName = " + packageName);
            ProcessUtil.runWithClearedIdentity(new Runnable() {
                public void run() {
                    PblNotificationProcessor.processNotificationRemoval(packageName);
                }
            });
        } catch (Exception e) {
            Trace.error(TAG, "Error handling sidechannel notification", e);
            MobileAppBehavior.logNotificationServiceError(TAG, false, e);
        } catch (Throwable t) {
            Trace.error(TAG, "Unrecoverable error occurred handling sidechannel notification", t);
            MobileAppBehavior.logNotificationServiceError(TAG, true, t);
        }
    }
}
